<?php
class Order_base_model extends CI_Model
{
    private $table   = 'order_base';
    private $table_1 = 'order_edit';
    private $table_2 = 'goods_base';

    public function updateBitPayByOrderId($order_id)
    {
    	$data = array(
    		  'bit_pay' => '65536'
    	);
    	$this->db->where('order_id',$order_id);
    	return $this->db->update($this->table,$data);
    }
    
    
    public function getOrderBaseByOrderId($order_id)
    {
       $this->db->where('order_id',$order_id);
       return $this->db->get($this->table);
    }
    
    /**
     * 根据orderiD 和 goodsId 查找
     */
    public function findByGoodsId($orderId, $GoodsId)
    {
        $this->db->where('goods_id', $GoodsId);
        $this->db->where('order_id > ', $orderId);
        $this->db->where('bit_pay & 65536 = 65536');
        $this->db->order_by('order_id', 'desc');
        return $this->db->get($this->table);
    }

    /**
     * 查找订单详细信息
     */
    public function findById($orderId, $payerUid)
    {
        $this->db->select('*');
        // $this->db->select('order_id,goods_name, payer_name, goods_num, play_name, sfz, phone, price, amount_fee, ordertime, plantime, other_id,  bit_pay, bit_opt, bit_wan, bit_use, goods_id, sid, status, usetime, calculate_method, number');
        $this->db->from($this->table);
        $this->db->where('order_id', $orderId);
        $this->db->where('payer_uid', $payerUid);
        $this->db->limit(1);
        return $this->db->get();
    }

    /**
     * 分页 总页数
     */
    public function total($params=array())
    {
        $this->db->select('order_base.order_id');
        $this->db->from($this->table.' AS order_base');
        $this->db->where('order_base.payer_uid', $params['payer_uid']);
        $this->db->where_in('order_base.cat_id', array(1,2,4,8,16));
        // $this->db->where('order_base.goods_num >', 0);
        if (!empty($params['order_status'])) {
            switch ($params['order_status']) {
                case 1: {//已取消订单
                    $this->db->where('order_base.bit_opt&128', 128);
                    break;
                }
                case 2: {//未付订单
                    $this->db->where('order_base.bit_pay', 0);
                    $this->db->where('order_base.bit_use', 0);
                    break;
                }
                case 3: {//已付未检订单
                    $this->db->where('(order_base.bit_pay & 65536) =', 65536);
                    $this->db->where('order_base.bit_use', 0);
                    break;
                }
                case 4: {//已检订单
                    $this->db->where('(order_base.bit_pay & 65536) =', 65536);
                    $this->db->where('order_base.bit_use >', 0);
                    break;
                }
                case 5: {//已评价订单（已改订单）
                    $this->db->where('(order_base.bit_pay & 65536) = ', 65536);
                    $this->db->where('order_base.bit_use >', 0);
                    break;
                }
                case 6: {//退款中订单
                    $this->db->select('order_edit.n_num, order_edit.sfz AS n_sfz, order_edit.plantime AS n_plantime, order_edit.n_amount_fee, order_edit.poundage, order_edit.amount_back, order_edit.flag');
                    $this->db->join($this->table_1.' AS order_edit', 'order_edit.order_id = order_base.order_id');
                    $this->db->where('(order_base.bit_opt&128)', 128);
                    $this->db->where('order_edit.flag', 1);
                    break;
                }
                case 7: {//已改订单
                    $this->db->select('order_edit.o_num, order_edit.o_amount_fee, order_edit.poundage, order_edit.amount_back, order_edit.flag, order_edit.created_at');
                    $this->db->join($this->table_1.' AS order_edit', 'order_edit.n_order_id = order_base.order_id');
                    $this->db->where('order_edit.flag', 2);
                    break;
                }
                default:break;
            }
        }
        return $this->db->count_all_results();
    }

    /**
     * 查询订单
     */
    public function page_list($params, $start, $count)
    {
        $this->db->select('order_base.pay_id, order_base.order_id, order_base.goods_name, order_base.payer_name, order_base.goods_num, order_base.amount_fee, order_base.ordertime, order_base.plantime, bit_pay, bit_opt, bit_wan, bit_use, status, usetime, calculate_method, is_edit, edit_day, edit_time, is_refund, is_partial_refund, refund_day, refund_time');
        $this->db->from($this->table.' AS order_base');
        $this->db->where('order_base.payer_uid', $params['payer_uid']);
        $this->db->where_in('order_base.cat_id', array(1,2,4,8,16));
        // $this->db->where('order_base.goods_num >', 0);
        if (!empty($params['order_status'])) {
            switch ($params['order_status']) {
                case 1: {//已取消订单
                    $this->db->where('order_base.bit_opt&128', 128);
                    break;
                }
                case 2: {//未付订单
                    $this->db->where('order_base.bit_pay', 0);
                    $this->db->where('order_base.bit_use', 0);
                    break;
                }
                case 3: {//已付未检订单
                    $this->db->where('(order_base.bit_pay & 65536) =', 65536);
                    $this->db->where('order_base.bit_use', 0);
                    break;
                }
                case 4: {//已检订单
                    $this->db->where('(order_base.bit_pay & 65536) =', 65536);
                    $this->db->where('order_base.bit_use >', 0);
                    break;
                }
                case 5: {//已评价订单（已改订单）
                    $this->db->where('(order_base.bit_pay & 65536) = ', 65536);
                    $this->db->where('order_base.bit_use >', 0);
                    break;
                }
                case 6: {//退款中订单
                    $this->db->select('order_edit.n_num, order_edit.sfz AS n_sfz, order_edit.plantime AS n_plantime, order_edit.n_amount_fee, order_edit.poundage, order_edit.amount_back, order_edit.flag');
                    $this->db->join($this->table_1.' AS order_edit', 'order_edit.order_id = order_base.order_id');
                    $this->db->where('(order_base.bit_opt&128)', 128);
                    $this->db->where('order_edit.flag', 1);
                    break;
                }
                case 7: {//已改订单
                    $this->db->select('order_edit.o_num, order_edit.o_amount_fee, order_edit.poundage, order_edit.amount_back, order_edit.flag, order_edit.created_at');
                    $this->db->join($this->table_1.' AS order_edit', 'order_edit.n_order_id = order_base.order_id');
                    $this->db->where('order_edit.flag', 2);
                    break;
                }
                default:break;
            }
        }
        $this->db->order_by('order_base.order_id', 'DESC');
        $this->db->limit($count, $start);
        return $this->db->get();
    }

    /**
     * 添加订单信息
     * @param unknown $orderData 订单数据
     */
    public function add($orderData)
    {
        $data = array(
            'old_order_id' => $orderData->old_order_id,
            'mid' => $orderData->mid,
            'cat_id' => $orderData->cat_id,
            'goods_id' => $orderData->goods_id,
            'goods_num' => $orderData->n_num,
            'payer_uid' => $orderData->payer_uid,
            'payer_name' => $orderData->payer_name,
            'bit_use' => $orderData->bit_use,
            'bit_pay' => $orderData->bit_pay,
            'bit_opt' => $orderData->bit_opt,
            'bit_wan' => $orderData->bit_wan,
            'goods_name' => $orderData->goods_name,
            //'goods_short_name' => $orderData->goods_short_name,
            'provider_name' => $orderData->provider_name,
            'add_rank' => $orderData->add_rank,
            'play_name'=>$orderData->play_name,
            'sfz' => $orderData->tsfz,
            'phone' => $orderData->phone,
            'price' => $orderData->price,
            'number' => $orderData->n_num,
            'amount_total' => $orderData->amount_total_n,
            'amount_fee' => $orderData->amount_fee_n,
            'amount_bouns' => $orderData->amount_bouns,
            'amount_provide' => $orderData->amount_provide,
            'amount_second_provide' => $orderData->amount_second_provide,
            'pay_method' => $orderData->pay_method,
            'ordertime' => $orderData->ordertime,
            'paytime' => $orderData->paytime,
            'planTime' => $orderData->tplantime,
            'usetime' => $orderData->usetime,
            'start_time' => $orderData->start_time,
            'end_time' => $orderData->end_time,
            'mac_ids' => $orderData->mac_ids,
            'other_id' => $orderData->other_id,
            'out_other_id' => $orderData->out_other_id,
            'out_other_id_is_new' => $orderData->out_other_id_is_new,
            'api_is_new_or_old' => $orderData->api_is_new_or_old,
            'sid' => $orderData->sid,
            'parent_other_id' => $orderData->parent_other_id,
            'parent_order_id' => $orderData->parent_order_id,
            'audit_id' => $orderData->audit_id,
            'pay_id' => $orderData->pay_id,//支付记录id
            'api_id' => $orderData->api_id,//接口id
            'is_edit' => $orderData->is_edit,//是否可改 1-可改 2-不可改
            'is_refund' => $orderData->is_refund,//是否可退 1-可退 2-不可退
            'is_partial_refund' => $orderData->is_partial_refund,//是否部分可退 1-是 2-否
            'refund_day' => $orderData->refund_day,//退票天数限制
            'refund_time' => $orderData->refund_time, //退票时间限制
            'calculate_method' => $orderData->calculate_method, //是分润订单还是议价订单 1-分润 2-议价
            'order_source' => $orderData->order_source //订单来源 1-PC端 2-手机端
        );
    
        $this->db->insert($this->table, $data);
        return $this->db->insert_id();
    }

    /**
     * 删除订单信息
     * @param unknown $where
     */
    public function delete($order_id)
    {
        $this->db->where('order_id', $order_id);
        return $this->db->delete($this->table);
    }

     /**
     * 修改订单信息
     */
    public function update_set($where,$data)
    {
        if(isset($data['bit_opt']) && $data['bit_opt'] != NULL) {
            $bit_opt = $data['bit_opt'];
            $this->db->set('bit_opt',"bit_opt-$bit_opt",FALSE);
        }
        $this->db->where($where);
    
        return $this->db->update($this->table);
    }

    
    public function findGoodsOrder($orderId)
    {
        $this->db->select('order_base.*,advance_date,advance_time,o_api_id,o_goods_id,other_scen_id');
        $this->db->from($this->table.' AS `order_base`');
        $this->db->join($this->table_2.' AS `goods_base`', 'order_base.goods_id=goods_base.goods_id','INNER');
        $this->db->where('order_base.order_id', $orderId);
        $this->db->limit(1);
        return $this->db->get();
    }

    public function orderRefund($orderId)
    {
        $this->db->set('bit_opt', 'bit_opt | 128',false);
        $this->db->where('order_id',$orderId);
        return $this->db->update($this->table);
    }

    /**
     * 更改订单信息
     */
    public function updateInfo($data, $orderId)
    {
        $this->db->where('order_id', $orderId);
        return $this->db->update($this->table, $data);
    }
    
    public function findByParams($params=array())
    {
        if (!empty($params['select'])) {
            $this->db->select($params['select']);
        }
        if (!empty($params['sfz'])) {
            $this->db->where('sfz', $params['sfz']);
        }
        if (!empty($params['goods_id'])) {
            $this->db->where('goods_id', $params['goods_id']);
        }
        if (isset($params['bit_pay'])) {
            $this->db->where('bit_pay>', $params['bit_pay'], false);
        }
        if (isset($params['goods_num'])) {
            $this->db->where('goods_num>', $params['goods_num'], false);
        }
        if (isset($params['pay_id'])) {
            $this->db->where('pay_id', $params['pay_id']);
        }
        $this->db->where(array('order_base.ordertime >=' => strtotime(date('Y-01-01').' 00:00:00')));
        $this->db->where(array('order_base.ordertime <=' => strtotime(date('Y-12-31').' 23:59:59')));
        $this->db->order_by('order_id', 'DESC');
        return $this->db->get($this->table);
    }
    
    public function findByPayId($pay_id)
    {
        $this->db->where('pay_id', $pay_id);
        return $this->db->get($this->table);
    }
    
    public function insertOrderBase($orderBase=array())
    {
        $this->db->insert($this->table, $orderBase);
        return $this->db->insert_id();
    }
    
    public function updateByOrderPayId($params=array())
    {
        $data = array();
        if (!empty($params['pay_method'])) {
            $data['pay_method'] = $params['pay_method'];
        }
        if (!empty($params['bit_pay'])) {
            //$this->db->set('bit_pay', 'bit_pay+'.$params['bit_pay'], FALSE);
            $data['bit_pay'] = $params['bit_pay'];
        }
        if (!empty($params['paytime'])) {
            //$this->db->set('bit_pay', 'bit_pay+'.$params['bit_pay'], FALSE);
            $data['paytime'] = $params['paytime'];
        }
        if (!empty($params['payer_uid'])) {
            $this->db->where('payer_uid', $params['payer_uid']);
        }
        $this->db->where('pay_id', $params['pay_id']);
        return $this->db->update($this->table, $data);
    }
    
    /**
     * 查找最大订单号
     */
    public function findMaxOrderId($payId, $sid)
    {
        $this->db->select('order_id');
        $this->db->from($this->table);
        $this->db->where('pay_id > ',$payId);
        $this->db->where('sid', $sid);
        $this->db->where('bit_pay & 65536 = 65536');
        $this->db->order_by('order_id','desc');
        $this->db->limit(1);
        return $this->db->get();
    }
    
    /**
     * 获取最近订单
     * @param unknown $params
     * @param string $select
     * @param number $limit
     * @param string $orderBy
     */
    public function findByRecently($params=array(), $select='*', $limit=1, $orderBy='DESC')
    {
        $this->db->select($select);
        if (!empty($params['payer_uid'])) {
            $this->db->where('order_base.payer_uid', $params['payer_uid']);
        }
        $this->db->limit($limit);
        $this->db->order_by('order_id', $orderBy);
        return $this->db->get($this->table);
    }
}